package com.quan.xczs.rpc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Condition;

import com.quan.base.common.base.service.support.BaseService;
import com.quan.xczs.rpc.api.XczsConfigService;
import com.quan.xczs.rpc.dao.mapper.XczsConfigMapper;
import com.quan.xczs.rpc.model.XczsConfig;

@Service
public class XczsConfigServiceImpl extends BaseService<XczsConfig, Integer> implements XczsConfigService {
	
	@Autowired
	private XczsConfigMapper mapper ;

	@Override
	public XczsConfig load() {
		List<XczsConfig> list = super.selectAll() ;
		if(null != list && !list.isEmpty()) {
			return list.get(0);
		} else {
			XczsConfig config = new XczsConfig() ;
			
			return super.insertSelective(config);
		}
	}

	@Override
	public void setCouponEmpty(Integer couponId) {
		Condition condition = new Condition(XczsConfig.class) ;
		condition.createCriteria().andEqualTo("couponId", couponId);
		List<XczsConfig> selectByCondition = this.mapper.selectByCondition(condition);
		
		if(null != selectByCondition && !selectByCondition.isEmpty()) {
			XczsConfig xczsConfig = selectByCondition.get(0) ;
			
			//将关联的优惠券清空
			xczsConfig.setCouponId(null);
			super.update(xczsConfig) ;
		}
		
	}

	@Override
	public XczsConfig getConfig() {
		List<XczsConfig> list = super.selectAll() ;
		if(null != list && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
	
	

}
